<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Combo;
use App\Http\Validators\Request;
use App\Models\BaseModel;
use App\Models\Camp;
use App\Models\Food;
use App\Models\Filter;
use App\Models\Label;
use App\Models\Lease;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Show;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;

class ComboController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Combo(), function (Grid $grid) {
            $grid->model()->latest('id');
            $grid->column('id')->sortable();
            $grid->column('combo_type')->using(\App\Models\Combo::typeMap());
            $grid->column('combo_name');
            $grid->column('combo_img')->display(function ($img) {
                $i = Storage::url(explode(',', $img)[0] ?? '');
                return "<img src={$i} width='50'>";
            });
            $grid->column('combo_price');
            $grid->column('start_time');
            $grid->column('end_time');
            $grid->column('stock');
            $grid->column('sales');
            $grid->column('views');
            $grid->column('sort')->sortable();
            $grid->column('status')->switch();
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                $filter->equal('combo_type')->select(\App\Models\Combo::typeMap());
                $filter->like('combo_name');
                $filter->between('start_time')->datetime();
                $filter->between('end_time')->datetime();
                $filter->equal('status')->select(BaseModel::statusMap());
                $filter->between('created_at')->datetime();
                $filter->between('updated_at')->datetime();
            });
            $grid->quickSearch(['id', 'combo_name']);
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Combo(), function (Show $show) {
            $show->field('id');
            $show->field('camp_id');
            $show->field('combo_type');
            $show->field('combo_name');
            $show->field('combo_desc');
            $show->field('combo_img');
            $show->field('combo_price');
            $show->field('combo_con');
            $show->field('filter_ids');
            $show->field('label_ids');
            $show->field('buy_notice');
            $show->field('other_notice');
            $show->field('start_time');
            $show->field('end_time');
            $show->field('time_slot');
            $show->field('stock');
            $show->field('sales');
            $show->field('views');
            $show->field('sort');
            $show->field('status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Combo(), function (Form $form) {
            $form->display('id');
            $form->radio('combo_type')->options(\App\Models\Combo::typeMap())->default(\App\Models\Combo::TYPE_A);
            $form->text('combo_name')->rules('required|max:100');
            $form->text('combo_desc')->rules('required');
            $form->multipleImage('combo_img')->autoUpload()->removable(false)->retainable()->saving(function ($value) {
                return implode(',', $value);
            })->rules('required');
            $form->decimal('combo_price')->rules('required');
            $form->table('combo_con', function ($table) use ($form) {
                $table->select('sell_type', '类型')->options(\App\Models\Combo::sellTypeMap())->load('sell_id', '/combo/selectSellId');
                $table->select('sell_id', '选择');
                $table->decimal('sell_price', '价格');
            })->saving(function ($v) {
                return json_encode($v);
            });
            $form->multipleSelect('filter_ids')->options(Filter::all()->where('filter_type', Filter::TYPE_COMBO)->pluck('filter_name', 'id'))->saving(function ($value) {
                return implode(',', $value);
            });
            $form->multipleSelect('label_ids')->options(Label::all()->pluck('label_name', 'id'))->saving(function ($value) {
                return implode(',', $value);
            });
            $form->keyValue('buy_notice')->setKeyLabel('标题')->setValueLabel('内容')->saveAsJson();
            $form->editor('other_notice');
            $form->datetimeRange('start_time', 'end_time', '活动时间');
            $form->number('stock')->type('number');
            $form->number('sort')->type('number');
            $form->radio('status')->options(BaseModel::statusMap())->default(BaseModel::STATUS_DISABLE);
        });
    }

    protected function selectSellId(Request $request)
    {
        $sellType = $request->get('q');

        if ($sellType == \App\Models\Combo::SELL_CAMP) {
            return Camp::query()->where('status', BaseModel::STATUS_ENABLE)->get(['id', DB::raw('camp_name as text')]);
        } elseif ($sellType == \App\Models\Combo::SELL_LEASE) {
            return Lease::query()->where('status', BaseModel::STATUS_ENABLE)->get(['id', DB::raw('lease_name as text')]);
        } elseif ($sellType == \App\Models\Combo::SELL_FOOD) {
            return Food::query()->where('status', BaseModel::STATUS_ENABLE)->get(['id', DB::raw('food_name as text')]);
        }
    }
}
